package simulateOS.management.process;

import javafx.beans.property.*;
import javafx.scene.control.ProgressBar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import simulateOS.controller.ProcessPageController;
import simulateOS.entity.File;
import simulateOS.entity.PCB;
import simulateOS.entity.Status;
import simulateOS.management.device.DeviceManager;
import simulateOS.management.memory.MemoryManager;
import simulateOS.utils.RuntimeTimer;

import javax.annotation.Resource;
import java.util.concurrent.Executor;

/**
 * Description：用于创建进程ProcessRunner的工厂
 * <p>Date: 2022/11/2
 * <p>Time: 21:57
 *
 * @Author SillyBaka
 **/

@Slf4j
public class ProcessRunnerFactory {
    private MemoryManager memoryManager;

    private ProcessPageController processPageController;

    private RuntimeTimer runtimeTimer;

    private ProcessManager processManager;

    private DeviceManager deviceManager;


    //    @Autowired
    private Executor executor;

    public ProcessRunnerFactory(){

    }
    public ProcessRunner createProcessRunner(File executableFile){
        //PCB在调用的时候设定，防止重复
//        this.file = file;
        ProcessRunner processRunner = new ProcessRunner(executableFile);
        PCB pcb = memoryManager.getFreePCB();
        if(pcb == null){
            log.info("无法获取空闲进程块，创建进程失败");
            return null;
        }
        int pcbId = pcb.getPid();

        processRunner.setPcb(pcb);

//        IntegerProperty size = new SimpleIntegerProperty(executableFile.getSize());
        processRunner.setSize(executableFile.getSize());;//进程内存大小

        processRunner.setDataNum(executableFile.getSize());
        processRunner.setProgressBar(new ProgressBar());//设置初始进度条

        TimeRunner timeRunner = new TimeRunner(processRunner,runtimeTimer);
        processRunner.setTimeRunner(timeRunner);
        executor.execute(timeRunner);

        processRunner.setSubmitTime(runtimeTimer.getCurTime());

        processRunner.setProcessManager(processManager);
        processRunner.setMemoryManager(memoryManager);
        processRunner.setDeviceManager(deviceManager);
        processRunner.setProcessPageController(processPageController);

        if(processPageController.getReadyProcessList().size()>=10) {
            //由于等待就绪队列而被阻塞
            processRunner.setStatus(Status.WAIT);
            processPageController.getBlockingProcessList().add(processRunner);
            memoryManager.getMemory(processRunner);//申请内存
        } else {
            //正常进入就绪队列
            processRunner.setStatus(Status.READY);
            processPageController.getReadyProcessList().add(processRunner);
            memoryManager.getMemory(processRunner);//申请内存
        }

        return processRunner;
    }

    public void setMemoryManager(MemoryManager memoryManager) {
        this.memoryManager = memoryManager;
    }

    public void setProcessPageController(ProcessPageController processPageController) {
        this.processPageController = processPageController;
    }

    public void setRuntimeTimer(RuntimeTimer runtimeTimer) {
        this.runtimeTimer = runtimeTimer;
    }

    public void setExecutor(Executor executor) {
        this.executor = executor;
    }

    public void setProcessManager(ProcessManager processManager) {
        this.processManager = processManager;
    }

    public void setDeviceManager(DeviceManager deviceManager) {
        this.deviceManager = deviceManager;
    }
}
